<template>
  <div id="userLogin">
    <h2 class="title">AI零代码生成平台 - 登录</h2>
    <div class="desc">很牛的代码生成平台</div>
    <a-form :model="formState" name="basic" autocomplete="off" @finish="onSubmit">
      <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入账号！' }]">
        <a-input v-model:value="formState.userAccount" placeholder="请输入账号" allow-clear />
      </a-form-item>

      <a-form-item
        name="userPassword"
        :rules="[
          { required: true, message: '请输入密码！' },
          { min: 8, message: '密码长度不能小于8位！' },
        ]"
      >
        <a-input-password
          v-model:value="formState.userPassword"
          placeholder="请输入密码"
          allow-clear
        />
      </a-form-item>

      <div class="tip">
        没有账号
        <RouterLink to="/user/register">去注册</RouterLink>
      </div>

      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">登录</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { useLoginUserStore } from '@/stores/loginUser'
import { reactive } from 'vue'
import { userLogin } from '@/api/userController'
import { message } from 'ant-design-vue'
import router from '@/router'

const formState = reactive<API.UserLoginRequest>({
  userAccount: 'jingliu',
  userPassword: '12345678',
})

const loginUserStore = useLoginUserStore()

/**
 * 登录
 * @param values 登录表单数据
 */
const onSubmit = async (values: any) => {
  console.log('Success:', values)
  const res = await userLogin(values)
  if (res.data.code === 0 && res.data.data) {
    loginUserStore.setLoginUser(res.data.data)
    message.success('登录成功！')
    await router.push({
      path: '/',
      replace: true,
    })
  } else {
    message.error('登录失败,' + res.data.message)
  }
}
</script>

<style scoped>
#userLogin {
  max-width: 360px;
  margin: 0 auto;
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
}

.title {
  text-align: center;
  font-size: 24px;
  margin-bottom: 15px;
}
.desc {
  text-align: center;
  font-size: 14px;
  color: #666;
  margin-bottom: 15px;
}

.tip {
  text-align: right;
  font-size: 13px;
  color: #666;
  margin-bottom: 15px;
}

.tip a {
  color: #1890ff;
  text-decoration: none;
}

.tip a:hover {
  text-decoration: underline;
}
</style>
